
import { Directive, HostListener, ElementRef, Input } from "@angular/core";
import {Platform} from 'ionic-angular';
import $ from 'jquery'


@Directive({
    selector: "ion-textarea[autoresize]" // Attribute selector
})
export class AutosizeDirective {

    @HostListener('input', ['$event.target'])
    onInput(textArea: HTMLTextAreaElement): void {
        console.log(textArea,'hh');
        this.adjust();
    }

    @Input('autoresize') maxHeight: number;

    constructor(public element: ElementRef, private platform: Platform) {
    }

    ngAfterContentChecked(): void {
        this.adjust();
    }

    adjust(): void {
        let ta = this.element.nativeElement.querySelector("textarea"),
            newHeight;

        if (ta) {
            ta.style.overflow = "scroll";
            ta.style.height = "auto";
            if (this.maxHeight) {
                newHeight = Math.min(ta.scrollHeight, this.maxHeight);
            } else {
                newHeight = ta.scrollHeight;
            }
            ta.style.height = newHeight + "px";
        }
    }
}
